package com.luxsoft.siipap.em.replica.importadores.cxc.parches;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.HibernateException;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.luxsoft.siipap.model.CantidadMonetaria;
import com.luxsoft.siipap.model.core.ClienteCredito;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.util.DBUtils;

/**
 * Actualiza los datos mas recientes de clientes credito
 * 
 * @author Ruben Cancino Ramos
 *
 */
public class AjusteDeClientesCredito {
	
	public void execute(){
		final List<Map<String, Object>> rows=ServiceLocator2
			.getAnalisisJdbcTemplate().queryForList("select * from sw_clientes_credito");
		ServiceLocator2.getHibernateTemplate().execute(new HibernateCallback(){
			
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				ScrollableResults rs=session.createQuery("from ClienteCredito ").scroll();
				while(rs.next()){
					final ClienteCredito credito=(ClienteCredito)rs.get()[0];
					System.out.println("Procesando client: "+ToStringBuilder.reflectionToString(credito));
					//Localizando cliente
					
					Object found=CollectionUtils.find(rows, new Predicate(){
						public boolean evaluate(Object input) {
							Map<String, Object> row=(Map<String, Object>)input;
							String clave=(String)row.get("CLAVE");
							if(clave!=null && (credito.getCliente()!=null))
								return clave.equals(credito.getCliente().getClave());
							return false;
						}						
					});
					if(found!=null){
						
						Map<String,Object> row=(Map<String, Object>)found;
						
						boolean chequePostfechado=((Number)row.get("CHEQUEP")).intValue()==1;
						credito.setChequePostfechado(chequePostfechado);
						
						boolean conNotaAnticipada=((Number)row.get("CNOTA")).intValue()==1;
						credito.setConNotaAnticipada(conNotaAnticipada);
						
						//credito.setDescuentoEstimado(descuentoEstimado);
						Number diaCobro=(Number)row.get("DIA_PAGO");
						credito.setDiacobro(diaCobro.intValue());
						
						Number diaRvision=(Number)row.get("DIA_REVISION");
						credito.setDiarevision(diaRvision.intValue());
						
						BigDecimal linea=(BigDecimal)row.get("LIMITE");
						credito.setLinea(CantidadMonetaria.pesos(linea.doubleValue()));
						
						Number plazo=(Number)row.get("PLAZO");
						credito.setPlazo(plazo.intValue());
						
						
						//credito.setSuspenderDescuento(suspenderDescuento);
						//credito.setSuspendido(suspendido);
						
						//Number vencimientoFactura=(Number)row.get("TIPO_VENCIMIENTO");
						credito.setVencimientoFactura(true);
						credito.setRevision(true);
						
					}else{
						System.out.println("No existe registro ");
					}
				}
				return null;
			}			
		});
			
	}
	
	public static void main(String[] args) {
		DBUtils.whereWeAre();
		new AjusteDeClientesCredito().execute();
	}

}
